<%-- 
    Document   : index
    Created on : 18 juin 2012, 15:27:15
    Author     : pascal
--%>

<%@page import="java.util.Random"%>
<%@page import="sun.misc.BASE64Encoder"%>
<%@page import="javax.crypto.Mac"%>
<%@page import="javax.crypto.spec.SecretKeySpec"%>
<%@page import="beans.InfoSyncStorage"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>


<%
    InfoSyncStorage store = new InfoSyncStorage();
    String verifier = request.getParameter("oauth_verifier");
    store.setVerifier(verifier);
    Random rand = new Random();
    int nomus = rand.nextInt(99999999);

    long time = System.currentTimeMillis();
    time = time / 1000;
    time = time - 10;


    String requete = "GET&http%3A%2F%2Fwww.flickr.com%2Fservices%2Foauth%2Faccess_token"
            + "&oauth_consumer_key%3D" + store.getAppKey()
            + "%26oauth_nonce%3D" + nomus
            + "%26oauth_signature_method%3DHMAC-SHA1"
            + "%26oauth_timestamp%3D" + time
            + "%26oauth_token%3D" + store.getTokenRequest()
            + "%26oauth_verifier%3D" + store.getVerifier()
            + "%26oauth_version%3D1.0";
    String key = store.getAppSecret() + "&" + store.getTokenSecret();
    SecretKeySpec keySpec = new SecretKeySpec(
            key.getBytes(),
            "HmacSHA1");

    Mac mac = Mac.getInstance("HmacSHA1");
    mac.init(keySpec);
    byte[] result = mac.doFinal(requete.getBytes());

    BASE64Encoder encoder = new BASE64Encoder();
    String sig = encoder.encode(result);
    System.out.println(sig);

    String lien = "http://www.flickr.com/services/oauth/access_token"
            + "?oauth_nonce=" + nomus
            + "&oauth_timestamp=" + time
            + "&oauth_verifier=" + store.getVerifier()
            + "&oauth_consumer_key=" + store.getAppKey()
            + "&oauth_signature_method=HMAC-SHA1"
            + "&oauth_version=1.0"
            + "&oauth_token=" + store.getTokenRequest()
            + "&oauth_signature=" + sig;
%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Test flickr</title>
    </head>
    <body>
        <h1>Retour</h1>
        <p>
        <h3>les parametres</h3>
        <%
            out.println(store.toString());
        %>

    </p>
    <a href="<%= lien%>">acces</a>
</body>
</html>
